package kentriko;

import java.io.File;
import java.util.Hashtable;
import javax.swing.JOptionPane;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.*;

/**
 *
 * @author dimitris
 */
public class Arxiki extends javax.swing.JFrame {

    Hashtable myHt = new Hashtable();

    public Arxiki() {
        initComponents();
    }

    public void setDetails() {

        try {
            File file = new File("temp.xml");
            //File file = new File(url);
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(file);
            //doc.getDocumentElement().normalize();
            NodeList video = doc.getElementsByTagName("video");
            Node fstNode = video.item(0);

            jTextArea1.append(fstNode.getAttributes().getNamedItem("title").toString() + "\n\n");
            jTextArea1.append(fstNode.getAttributes().getNamedItem("length").toString() + "\n");
            jTextArea1.append(fstNode.getAttributes().getNamedItem("size").toString() + "\n");
            jTextArea1.append(fstNode.getAttributes().getNamedItem("username").toString() + "\n");
            jTextArea1.append(fstNode.getAttributes().getNamedItem("tags").toString() + "\n");
            jTextArea1.append(fstNode.getAttributes().getNamedItem("views").toString() + "\n");
            jTextArea1.append(fstNode.getAttributes().getNamedItem("rating").toString() + "\n");
            jTextArea1.append(fstNode.getAttributes().getNamedItem("numRatingVotes").toString() + "\n");
            jTextArea1.append(fstNode.getAttributes().getNamedItem("description").toString() + "\n");
            jTextArea1.append(fstNode.getAttributes().getNamedItem("dateAdded").toString() + "\n");
            jTextArea1.append(fstNode.getAttributes().getNamedItem("age").toString() + "\n");



        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error parsing the XML");
            e.printStackTrace();
        }

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        jButton1 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jProgressBar1 = new javax.swing.JProgressBar();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("VeohGrabber 2.0");

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        jButton1.setText("Download");
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton1);

        jButton3.setText("Exit");
        jButton3.setFocusable(false);
        jButton3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton3.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton3);
        jToolBar1.add(jProgressBar1);

        jLabel1.setText("Welcome to VeohGraber 2.0");

        jTextField1.setText("http://www.veoh.com/browse/videos/category/music_electronic/watch/v16129239qzmpJhRY");
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jTextArea1.setColumns(20);
        jTextArea1.setLineWrap(true);
        jTextArea1.setRows(5);
        jTextArea1.setWrapStyleWord(true);
        jScrollPane1.setViewportView(jTextArea1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 427, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 407, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 407, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 407, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 254, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        jButton1.setEnabled(false);
        String url = jTextField1.getText();
        if (url != "") {
            if (url.contains("veoh.com/")) {
                grab g = new grab(url, jProgressBar1, jLabel1);
                this.setDetails();
                //g.run();
                try {
                    jProgressBar1.setIndeterminate(true);
                    jLabel1.setText("Downloading...");
                    FileDownload down = new FileDownload(g.doInBackground(), "videaki.mp4", jLabel1);
                    down.execute();
                    if (down.isDone()) {
                        jProgressBar1.setIndeterminate(false);
                        jLabel1.setText("Finished.");
                        jButton1.setEnabled(true);
                    }
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(this, "Error downloading the final file");
                }
            } else {
                JOptionPane.showMessageDialog(this, "Invalid Url");
            }
        } else {
            JOptionPane.showMessageDialog(this, "Please provide a Url");
        }

    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed

        System.exit(0);

    }//GEN-LAST:event_jButton3ActionPerformed

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Arxiki().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JProgressBar jProgressBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JToolBar jToolBar1;
    // End of variables declaration//GEN-END:variables
}
